<?php
/**
 * @file name : weblink.php
 * @package   : thokim
 * @author    : Vo Thanh Minh
 * @email     : <thanhminhld@yahoo.com - thanhminhld@gmail.com>
 * @copyright : 2011
 *
 */
if (!defined('BASEPATH')) {
    exit('No direct script access');
}

class Weblink extends Base_Controller
{
    /**
     * __construct
     * @access public
     * @param
     * @return
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
      * _remap
      * @access public
      * @param
      * @return
      */
    public function _remap( $method )
    {
        switch ($method) {

            case 'add':
            case 'edit':
                $this->addEditWeblink();
                break;

            case 'save':
                $this->saveWeblink();
                break;

            case 'del':
                $this->delWeblink();
                break;

            default:
                $this->index();
        }
    }
    /**
     * @function index
     * @access     private
     * @param
     * @return
     */
    private function index()
    {
        $controlName    = $this->input->get_post('c');
        /**
         * Get message when save insert or edit submit
         *
         */
        $msgBoxDisp = '';
        $clsType    = '';
        $clsIcon    = '';

        $messages = $this->getErrMsg($msgBoxDisp, $clsType, $clsIcon);
        /**
         * Config for paging
         */
        $arrWhere = array('lang_id' => $this->lang_default);
        $this->load->library('pagination');
        $config['base_url'] = 'http://' . $_SERVER["HTTP_HOST"] . dirname($_SERVER["SCRIPT_NAME"])
                            . '/?c=' . $controlName;
        $totalRows = $this->weblink_model->getTotalRows($arrWhere);
        $config['total_rows'] = $totalRows;
        $config['per_page'] = 10;
        $config['num_links'] = 2;
        $config['page_query_string'] = TRUE;

        $config['first_link'] = 'First';
        $config['last_link'] = 'Last';
        $config['next_link'] = 'Next';
        $config['prev_link'] = 'Previous';
        $config['cur_tag_open'] = '&nbsp;<font color=#FF0000><strong>';
        $config['cur_tag_close'] = '</strong></font>';

        $this->pagination->initialize($config);
        $str_paging = $this->pagination->create_links();
        $this->msmarty->assign('paging',$str_paging);

        $offset = isset($_GET['per_page']) ? $_GET['per_page'] : 0;
        /**
         * End paging
         */
        $modulesList = $this->weblink_model->loadList($arrWhere, $config['per_page'], $offset);

        $data = array('urlControlName'  => '?c=' . $controlName,
                      'modulesList'        => $modulesList,
                      'messages'        => $messages,
                      'clsType'         => $clsType,
                      'clsIcon'         => $clsIcon,
                      'msgBoxDisp'      => $msgBoxDisp);

        $this->msmarty->view($controlName . '/admin.weblink_manage', $data);
    }
    /**
     * @function addEditWeblink
     *
     * @author   Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access   private
     * @param
     * @return
     */
    private function addEditWeblink()
    {
        $controlName = $this->input->get_post('c');
        $langObj     = $this->lang_model->getLang();
        $web_id  = $this->input->get_post('web_id');
        /**
         * Get message when save insert or edit submit
         *
         */
        $msgBoxDisp = '';
        $clsType    = '';
        $clsIcon    = '';

        $messages = $this->getErrMsg($msgBoxDisp, $clsType, $clsIcon);
        /**
         * Load record for edit case
         */
        $records = array();
        if (is_numeric($web_id) && $web_id > 0) {
            $records = $this->weblink_model->loadRecord($web_id);
        }
        foreach ($langObj as $k => $vlang) {
            $arrLang[$k]['id'] = $vlang->id;
            $arrLang[$k]['name'] = $vlang->name;
            $arrLang[$k]['default'] = $vlang->default;
            foreach($records as $recd) {
                if ($recd->lang_id == $vlang->id) {
                    $arrLang[$k]['web_title'] = $recd->web_title;
                    $arrLang[$k]['web_link'] = $recd->web_link;
                }
            }
        }
        /**
         * Your code addedit here
         */
        $data = array('actionForm'      => '?c=' . $controlName . '&m=save',
                      'messages'        => $messages,
                      'clsType'         => $clsType,
                      'clsIcon'         => $clsIcon,
                      'msgBoxDisp'      => $msgBoxDisp,
                      'web_id'           => $web_id);
        $this->msmarty->assign('langList', $arrLang);
        $this->msmarty->view($controlName . '/admin.weblink_addedit', $data);
    }
    /**
     * @function saveWeblink
     *
     * @author   Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access   private
     * @param
     * @return
     */
    private function saveWeblink()
    {
        $controlName    = $this->input->get_post('c');
        if ($_SERVER['REQUEST_METHOD']==='POST') {
                       $data['web_title'] = $this->input->post('web_title');
           $data['web_link'] = $this->input->post('web_link');
			$web_id              = $this->input->post('web_id');

			if ($web_id > 0) {
				$fUpdate       = true;
				$data['web_id']  = $web_id;
			} else {
				$fUpdate       = false;
			}
			/**
			 * Load lang to show msg
			 */
			$this->lang->load('success', $this->lang_tag);
			$this->lang->load('error', $this->lang_tag);
			///// Start transaction /////
			/////////////////////////////
			$this->db->trans_begin();

			$ret = $this->weblink_model->store($data, 'web_id', true, $fUpdate);

			if ($ret == FALSE) {
			    $this->messages->add($this->lang->line('db_insert_update_err'), 'error');
			    // Rollback transaction
			    $this->db->trans_rollback();
			} else {

                if ($fUpdate) {
                    $this->messages->add($this->lang->line('db_update_success'), 'success');
                } else {
                    $this->messages->add($this->lang->line('db_insert_success'), 'success');
                }
                // Commit transaction
                $this->db->trans_commit();
			}

        }
        defaultPage($controlName);
    }
    /**
     * @function delWeblink
     *
     * @author     Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access     private
     * @param
     * @return     void
     */
    private function delWeblink()
    {
        $controlName    = $this->input->get_post('c');
        $web_id = $this->input->get_post('web_id');
        /**
         * Load lang to show msg
         */
        $this->lang->load('success', $this->lang_tag);
        $this->lang->load('error', $this->lang_tag);
        if ( ! is_numeric($web_id) || $web_id <= 0) {
            $this->messages->add($this->lang->line('value_stranfer_err'), 'error');
            defaultPage($controlName);
        }
        $this->weblink_model->deleteTbl(array('web_id' => $web_id), 'web_id');
        $this->messages->add($this->lang->line('db_delete_success'), 'success');
        defaultPage($controlName);
    }
    /**
     * @function getErrMsg
     *
     * @author   Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access   private
     * @param    string $msgBoxDisp, $clsType, $clsIcon
     * @return   array
     */
    private function getErrMsg(&$msgBoxDisp, &$clsType, &$clsIcon)
    {
        $messages = $this->messages->get();

        if (!is_array($messages)) {
            $messages = array();
            $msgBoxDisp = 'none;';
        } else {
            foreach($messages as $type => $message) {
                if(!empty($message)) {
                    if ($type == 'error') {
                        $clsType = $type;
                        $clsIcon = 'errorIcon';
                    } else if ($type == 'success') {
                        $clsType = $type;
                        $clsIcon = 'successIcon';
                    }
                    break;
                }
            }
        }
        $this->messages->clear();
        return $messages;
    }
}
